// ================================================================================
// 		File: PartyRepresentativeInfo.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（）
// 		Date: 2016-03-23 01:45
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// 
// ================================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ZHDJ.Core;

namespace ZHDJ.Data.Models.Center
{
    /// <summary>
    /// 党代表信息
    /// </summary>   
    public class PartyRepresentativeInfo : EntityBase
    {
        #region 属性
        /// <summary>
        /// 工作室ID
        /// </summary>
        public string StudioID { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 联系方式
        /// </summary>
        public string MobileNo { get; set; }

        /// <summary>
        /// 职务
        /// </summary>
        public string Post { get; set; }

        /// <summary>
        /// 电子邮箱
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// 居住地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 头像
        /// </summary>
        public string ImageID { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public int OrderNo { get; set; }

        /// <summary>
        /// 党代表类型（本单位、外单位）
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 本单位人员LoginID
        /// </summary>
        public string LoginID { get; set; }

        /// <summary>
        /// 创建用户
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改用户
        /// </summary>
        public string ModifyBy { get; set; }

        #endregion

        #region 查询管理

        public static DataSet GetGridData(int pageIndex, int pageSize, string category, string searchKey = "", string studioID = "")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  data.ID,data.StudioID,data.UserName,
                        data.MobileNo,data.Post,data.Email,data.Address,data.ImageID,
                        data.OrderNo,data.Category,data.LoginID,data.CreateBy,
                        data.CreateTime,data.ModifyTime,data.ModifyBy,b.StudioName,
                        case data.Category when 1 then '本单位' else '外单位' end as CategoryName
                        from tblPartyRepresentativeInfo data
                        left join tblPartyRepresentativeStudio b on(data.StudioID=b.ID)
                        where 1=1 ");
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and data.UserName like ?key ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "key", Value = string.Format("%{0}%", searchKey) });
            }
            if (!string.IsNullOrEmpty(category))
            {
                sb.Append(" and data.category=?category ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "category", Value = category });
            }
            if (!string.IsNullOrEmpty(studioID))
            {
                sb.Append(" and data.studioID=?studioID ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "studioID", Value = studioID });
            }
            sb.Append(" order by data.orderNo,data.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        /// <summary>
        /// 获取活动公告出席的党代表
        /// </summary>
        /// <param name="DB"></param>
        /// <param name="meetingId"></param>
        /// <returns></returns>
        public static string GetPartyRepresentativeJoin(AppBoxContext DB, string meetingId)
        {
            var q = DB.PartyRepresentativeMeetingJoins.Where(a => a.MeetingID == meetingId);
            var names = new List<string>();
            foreach (var item in q)
            {
                var join = DB.PartyRepresentativeInfos.Find(item.RepresentativeID);
                if (join != null)
                {
                    names.Add(join.UserName);
                }
            }
            return "【" + string.Join(",", names.ToArray()) + "】";
        }
        #endregion
    }
}
